﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DM4;
using sscore;

namespace DM4Client
{
    public partial class FrmAddEdit : XtraForm
    {
        private readonly string _frmCaption;
        private readonly string _lblText;
        public string TbText;
        public FrmAddEdit(string frmCaption, string lblText, string tbText)
        {
            InitializeComponent();
            _frmCaption = frmCaption;
            _lblText = lblText;
            TbText = tbText;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tb.Text))
            {
                MessageBox.Show(this, _lblText + " is empty.", DmConsts.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                tb.Focus();
                return;
            }
            TbText = tb.Text;
            DialogResult = DialogResult.OK;
            Close();
        }

        private void FrmAddEdit_Shown(object sender, EventArgs e)
        {
            Text = _frmCaption;
            lbl.Text = _lblText;
            tb.Text = TbText;
            tb.Focus();
        }
    }

    public static class TextBoxForm
    {
        public static int Show(string frmCaption, string description, ref string value)
        {
            int result = DM4Consts.ErrorUnknown;
            using (FrmAddEdit frmAddEdit = new FrmAddEdit(frmCaption, description, value))
            {
                DialogResult dr = frmAddEdit.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    value = frmAddEdit.TbText.Trim();
                    result = DM4Consts.Ok;
                }
            }
            return result;
        }
    }
}